import { ListBase } from 'src/components/list_base_abs';
import { Component, Emit, Prop, Provide } from 'vue-property-decorator'

import Creator from './tree_data';
import { OrganizationConst } from "src/consts/organization_const";
import { updateSelection } from 'src/utils/list_utils';
import OperatorHandler from 'src/views/org/operator_handler';
import ListDemoHandler from 'src/views/org/list_handler';
import { DemoTreeHandler } from 'src/views/demo_tree/demo_tree_handler';
@Component({})
export default class Org extends ListBase {
    operatorData: OperatorHandler;
    listHandler: ListDemoHandler;
    myState: any;
    mainData: any;
    queryData: any;

    initData(that: Org) {
        // that.myState = Creator.dataCreator();
        console.log(Creator.dataCreator(), "zoom", this.$store.state.org)
        that.myState = this.$store.state.org
        that.selection = [];
        that.operatorData = new OperatorHandler();
        that.listHandler = new ListDemoHandler();
        that.operatorData.init(this);
        that.mainData = {}
        that.queryData = {};
    }

    handleNodeClick(data: any) {
        console.log("click node", data);
    }

    getListData(): void {
        let sendData: any = {}
        sendData["curr_page"] = 1
        sendData["page_size"] = 15
        sendData.level = 0
        WLDispatch(OrganizationConst.GetOrgTreeData, sendData);
    }

    mounted(): void {
        this.getListData();
    }

    doQuery(isAdv: Boolean): void {
        WLCommit(OrganizationConst.CommitQuery, true)

        var obj: any = {}
        for (let key in this.mainData) {
            if (this.mainData[key]) {
                obj[key] = this.mainData[key]
                if (key == "name") {
                    obj[key] = this.mainData[key].name
                }
                if (key == "leader_list") {
                    if (isNoEmpty(this.mainData["leader_list"])) {
                        for (let i = 0; i < this.mainData["leader_list"].length; i++) {
                            for (let leader_listKey in this.mainData["leader_list"][i]) {
                                if (leader_listKey != "id") {
                                    delete this.mainData["leader_list"][i][leader_listKey]
                                }
                            }
                        }

                        obj[key] = this.mainData[key]
                    } else {
                        delete obj[key]
                    }
                }
            }
        }

        this.queryData = obj
        if (!isNoEmptyObject(obj)) {
            obj.level = 0
        }
        obj["curr_page"] = this.myState.curr_page
        obj["page_size"] = this.myState.page_size
        WLDispatch(OrganizationConst.GetOrgTreeData, obj);
    }


    sizeChange(size: number): void {
        let sendData: any = this.queryData
        sendData["curr_page"] = this.myState.curr_page
        sendData["page_size"] = size
        sendData.level = 0
        WLDispatch(OrganizationConst.GetOrgTreeData, sendData);
    }

    pageChange(page: number): void {
        let sendData: any = this.queryData
        sendData["curr_page"] = page
        sendData["page_size"] = this.myState.page_size
        sendData.level = 0
        WLDispatch(OrganizationConst.GetOrgTreeData, sendData);
    }


    handleSelectionChange(list: any[]): void {
        updateSelection(this.selection, list);
        console.log("this selection", this.selection, this.myState.dataList);
    }

    rowClassName(data: any): string {
        if (data.row.selected) {
            return 'selected_row';
        }
        return '';
    }


    rowEdit(row: any): void {
        if (this.listHandler && this.listHandler.rowEdit) {
            this.listHandler.rowEdit(row);
        } else {
            console.warn("todo 初始化和实现行内容编辑");
        }
    }

    rowAdd(row: any): void {
        if (this.listHandler && this.listHandler.rowAdd) {
            this.listHandler.rowAdd(row);
        } else {
            console.warn("todo 初始化和实现行内容新增");
        }
    }

    rowDelete(row: any): void {
        if (row.level == 0) {
            WLMessage("顶级部门不能删除", "warning")
        }
        else {
            this.$confirm(`确定删除部门:${row.name}?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let sendData: any = {}
                sendData["id"] = row.id
                WLDispatch(OrganizationConst.DeleteTreeNode, sendData);
            }).catch(() => { })
        }
    }

    expand(row: any) {
        if (this.listHandler && typeof this.listHandler.expand === 'function') {
            this.listHandler.expand(row);
        } else {
            console.error("IDemoTreeHandler is NULL");
        }
    };
}